<?php
class Form_Adminlogin extends QForm
{
	function __construct($action)
	{
		// 调用父类的构造函数
		parent::__construct('form_adminlogin', $action);

		// 从配置文件载入表单
		$filename = rtrim(dirname(__FILE__), '/\\') . DS . 'adminlogin_form.yaml';
		$this->loadFromConfig(Helper_YAML::loadCached($filename));
		$this->addValidations(Acct::meta());
		$this['rndcode']->addValidations(array($this,'check_code'),'验证码错误!');
		$this['username']->addValidations(array($this,'check_user_name'),'账户不存在或被锁定不能登陆!');
		$this['userpass']->addValidations(array($this,'check_user_pass'),'您的账户或密码错误!');
	}
	/**
	 * 
	 * 验证码验证
	 */
	function check_code()
	{
		
		return Helper_Ckcode::check($this['rndcode']->value);
	}
	/**
	 * 
	 * 登陆账户确认
	 */
	function check_user_name()
	{
		$user_name = $this['username']->value;
		
		$user_obj = acct::find('acctname = ? and enable = 1',$user_name)->getOne();
		if ($user_obj->id()) {
			return true;
		}else{
		$user_obj = Acctuser::find('acctname = ? and enable = 1',$user_name)->getOne();
		if($user_obj->id())
		{	
			return true;
		}else{
			return false;
		}
		}
		//return false;
	}
	/**
	 * 
	 * 登陆账户密码
	 */
	function check_user_pass()
	{
		$user_name = trim($this['username']->value);
		$user_pass = trim($this['userpass']->value);
		
		if ($user_pass == '') return false;
		$user_obj = acct::find('acctname = ? and md5pass = ? and enable = 1',$user_name,md5($user_pass))->getOne();
		if ($user_obj->id()) {
			// 账户密码正确
			$_SESSION['acct'] = $user_obj->toArray();
			
			return true;
		}else{
			$user_obj = Acctuser::find('acctname = ? and password = ? and enable = 1',$user_name,($user_pass))->getOne();
			if($user_obj->id())
			{
			// 账户密码正确
			$_SESSION['acct'] = $user_obj->toArray();
			return true;
			}else{
			return false;
			}
		}
		//return false;
	}
}